package com.ynkbny.web.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ynkbny.domain.excel.MeterReadingExportVO;
import com.ynkbny.domain.pojo.DeviceExcelDataVO;
import com.ynkbny.lgapi.Device;
import com.ynkbny.domain.DeviceQram;
import com.ynkbny.domain.pojo.RegionTreeVO;

import com.ynkbny.lgapi.User;

import com.ynkbny.service.DeviceService;

import com.ynkbny.service.impl.DeviceServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RequestMapping("/test")
@RestController
public class TestController {
    @Resource
    User user;
    @Resource
    DeviceService deviceService;
    @Resource
    DeviceServiceImpl deviceService1;
    @Resource
    Device device;

    @GetMapping("/hello")
    public List<RegionTreeVO> hello() {
        return deviceService.buildRegionDeviceTree(572L);
    }

    @GetMapping("/list")
    public void getDataList(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), MeterReadingExportVO.class).build();
        String timestampStr = "1703779200";
        String timestampStr1 = "1703865600";
        // 10位时间戳
        long timestamp = Long.parseLong(timestampStr);
        long endstamp = Long.parseLong(timestampStr1);
        while (timestamp != 1706198400L) {
            List<MeterReadingExportVO> list = deviceService1.getDataList(572L, timestampStr, timestampStr1);

            // 创建一个Date对象，将时间戳传递给构造函数
            Date date = new Date(timestamp * 1000);
            Date date1 = new Date(endstamp * 1000);

            // 创建SimpleDateFormat对象，指定日期格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            // 使用SimpleDateFormat来格式化日期
            String formattedDate = sdf.format(date);
            WriteSheet writeSheet = EasyExcel.writerSheet(formattedDate).build();
            excelWriter.write(list, writeSheet);
            timestamp = timestamp + 86400;
            endstamp = endstamp + 86400;
            timestampStr = Long.toString(timestamp);
            timestampStr1 = Long.toString(endstamp);
        }
        excelWriter.finish();
    }
}
